<!-- subpkg_consult/order_list/components/order-list.vue -->
<script lang="ts" setup>
  import { orderListApi } from '@/apis/consult'
  import type { OrderItem } from '@/types/consult'
  import { ref } from 'vue'

  const props = defineProps<{ type: number }>()
  console.log(props.type)

  // 根据type获取列表数据 展示

  const current = ref(1) // 页码

  const list = ref<OrderItem[]>([])

  const status = ref<'more' | 'noMore' | 'loading'>('more')

  const getList = async () => {
    status.value = 'loading' // 加载中
    const res = await orderListApi({
      pageSize: '5',
      current: current.value.toString(),
      type: props.type.toString(),
    })
    console.log(res)
    // list.value = res.rows
    list.value.push(...res.rows) // 数据越加越多

    if (list.value.length === res.total) {
      // 数据没有
      more.value = false
      status.value = 'noMore' // 没有更多数据
      console.log('没数据了')
    } else {
      // 数据还有
      status.value = 'more' // 上拉显示更多
      current.value++ // 页码加1
    }
  }

  getList()

  const more = ref(true) // 默认有更多数据

  // 上拉加载
  const fn = () => {
    if (more.value) {
      // 有数据 加载更多
      getList()
    }
  }

  const isRefresh = ref(false)
  // 下拉刷新
  const fn2 = async () => {
    isRefresh.value = true // 处于下拉状态
    list.value = [] // 重置数据
    current.value = 1 // 重置页码

    await getList()
    isRefresh.value = false
  }
</script>

<template>
  <scroll-view
    @scrolltolower="fn"
    refresher-enabled
    @refresherrefresh="fn2"
    :refresher-triggered="isRefresh"
    refresher-background="#f6f6f6"
    class="uni-scroll-view"
    scroll-y
  >
    <view class="consult-list">
      <view class="consult-list-item" v-for="order in list" :key="order.id">
        <view class="consult-header">
          <view class="label">
            <image class="doctor-avatar" :src="order.docInfo?.avatar" />
            <text>{{ order.typeValue }}（自动分配医生）</text>
          </view>
          <text v-if="order.status === 1" class="status color-1">待支付</text>
          <text v-if="order.status === 2" class="status color-2">待接诊</text>
          <text v-if="order.status === 3" class="status color-2">咨询中</text>
          <text v-if="order.status === 4" class="status color-3">已完成</text>
          <text v-if="order.status === 5" class="status color-3">已取消</text>
        </view>
        <navigator
          class="consult-body"
          hover-class="none"
          :url="`/subpkg_consult/order_detail/index?id=${order.id}`"
        >
          <uni-list :border="false">
            <uni-list-item :border="false" title="病情描述" :right-text="order.illnessDesc" />
            <uni-list-item :border="false" title="价格" :right-text="'¥' + order.payment" />
            <uni-list-item :border="false" title="创建时间" :right-text="order.createTime" />
          </uni-list>
        </navigator>

        <view v-if="order.status === 1" class="consult-footer">
          <button class="uni-button minor">取消订单</button>
          <button class="uni-button">去支付</button>
        </view>
        <view v-if="order.status === 2" class="consult-footer">
          <button class="uni-button minor">取消订单</button>
          <button class="uni-button">继续咨询</button>
        </view>
        <view v-if="order.status === 3" class="consult-footer">
          <button class="uni-button">继续咨询</button>
        </view>
        <view v-if="order.status === 4" class="consult-footer">
          <button class="uni-button minor">问诊记录</button>
          <button v-if="order.evaluateId" class="uni-button minor">查看评价</button>
          <button v-else class="uni-button">去评价</button>
        </view>
        <view v-if="order.status === 5" class="consult-footer">
          <button class="uni-button minor">删除订单</button>
          <button class="uni-button">咨询其它医生</button>
        </view>
      </view>

      <!-- 加载状态 -->
      <uni-load-more
        :status="status"
        color="#C3C3C5"
        :icon-size="16"
        :content-text="{
          // status => more
          contentdown: '上拉显示更多',
          // status => loading
          contentrefresh: '数据正在加载中',
          // status => noMore
          contentnomore: '没有更多数据了',
        }"
      />
    </view>
  </scroll-view>
</template>

<style lang="scss">
  @import './styles.scss';
</style>
